package leetcode;

import java.util.HashMap;

public class Test0914 {
    public boolean hasGroupsSizeX(int[] deck) {
        HashMap<Integer,Integer> dic=new HashMap<Integer,Integer>();
        int minCount=-1;
        for(int i=0;i<deck.length;i++){
            if(dic.containsKey(deck[i])==false){
                dic.put(deck[i],1);
            }
            else{
                dic.put(deck[i],dic.get(deck[i])+1);
            }
        }
        int gongyueshu=-1;
        for(int i=0;i<deck.length;i++){
            if(gongyueshu==-1){
                gongyueshu=dic.get(deck[i]);//取得第一个count
            }
            else{
                gongyueshu=gongyueshu(gongyueshu,dic.get(deck[i]));//计算前一个count跟当前的公约数
                if(gongyueshu==1)//公约数为1
                    return false;
            }
        }
        if(gongyueshu<2)
            return false;
        else
            return true;
    }

    public int gongyueshu(int a,int b){
        int r = 1;
        do{
            r = a % b ;
            a = b;
            b = r;
            /*
            辗转相除法
            a对b取余，b赋值给a 余数赋值给b，，直到 b == 0
             */
        }while(b != 0);
        //System.out.println(a);
        return a;
    }
}
